#! /bin/sh
# Copyright (C) 2018 The Qt Company Ltd.
# Contact: http://www.qt.io/licensing/
#
# You may use this file under the terms of the 3-clause BSD license.
# See the file LICENSE from this package for details.
#

# Usage: in every repository you want to have checked:
#    cd .git/hooks
#    ln -s ../../../qtrepotools/git-hooks/clang-format-pre-commit pre-commit
#    mklink pre-commit ..\..\..\qtrepotools\git-hooks\clang-format-pre-commit
#

CLANG_FORMAT=$(which git-clang-format 2>/dev/null)
if [ -z "$CLANG_FORMAT" -a -d "$LLVM_INSTALL_DIR" ]; then
    export PATH=$PATH:$LLVM_INSTALL_DIR/bin
    CLANG_FORMAT=$(which git-clang-format 2>/dev/null)
fi

if test -z "$CLANG_FORMAT"; then
    echo "Please install clang-format and make sure git-clang-format is in the path."
    exit 0
fi

CLANG_FORMAT_DIFF=`git clang-format -q --diff`
if [ "$CLANG_FORMAT_DIFF" = "no modified files to format" ]; then
    exit 0
fi
if test -n "$CLANG_FORMAT_DIFF"; then
    echo "clang-format output:"
    echo "$CLANG_FORMAT_DIFF"
    echo "clang-format suggests changes in the format. You can run:"
    echo "  git clang-format HEAD~1"
    echo "and amend the commit with those changes that you agree with."
    exit 0
fi
